<template>
    <el-main>
        <div class="teacher">
            <div class="app-table">
                <div class="app-body">
                    <div class="app-search">
                        <el-form :inline="true" class="demo-form-inline" v-model="form">
                            <el-form-item label="学生姓名">
                                <el-input placeholder="请输入学生姓名" v-model="form.studentName"></el-input>
                            </el-form-item>
                            <el-form-item label="学生学号">
                                <el-input placeholder="请输入学生学号" v-model="form.studentNumber"></el-input>
                            </el-form-item>
                            <el-form-item label="校区" :label-width="formLabelWidth" prop="gradeId">
                                <el-select v-model="form.campus" autocomplete="off" @change="filterGradeId" clearable
                                    style="width: 192px;" placeholder="请选择校区">
                                    <el-option v-for="item in campusData" :key="item.value" :value="item.value"
                                        :label="item.label"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="所在年级" :label-width="formLabelWidth" prop="gradeId">
                                <el-select v-model="form.gradeId" autocomplete="off" @change="filterGradeId" clearable
                                    style="width: 192px;" placeholder="请选择所在年级">
                                    <el-option v-for="gradeId in gradeIds" :key="gradeId" :value="gradeId"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="所在班级" :label-width="formLabelWidth" prop="classId">
                                <el-select v-model="form.classId" autocomplete="off" placeholder="请选择所在班级" clearable
                                    style="width: 192px;">
                                    <el-option v-for="item in classInfos" :key="item.classId"
                                        :value="item.classId"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="导出时间">
                                <el-config-provider :locale="locale">
                                    <el-date-picker v-model="dateValue" type="daterange" range-separator="到"
                                        @change="changeDate" start-placeholder="请选择开始时间" end-placeholder="请选择结束时间" />
                                </el-config-provider>
                            </el-form-item>
                            <el-form-item>
                                <el-button class="serach_btn" type="primary" @click="onSubmit"
                                    @keyup.enter="searchEnter">搜索</el-button>
                                <el-button class="export_btn" type="warning" @click="exportExcel">导出报表</el-button>
                            </el-form-item>
                        </el-form>
                    </div>
                    <el-table :data="list" style="width: 100%" :row-class-name="tableRowClassName">
                        <el-table-column type="index" label="序号" width="80" />
                        <!-- <el-table-column prop="id" label="id" /> -->
                        <el-table-column prop="studentName" label="学生姓名" />
                        <el-table-column prop="studentNumber" label="学生学号" />
                        <el-table-column prop="typeName" label="离校/返校" />
                        <el-table-column prop="signTime" label="时间" />
                        <!-- <el-table-column prop="campus" label="校区" /> -->
                        <!-- <el-table-column label="操作" width="140px">
                            <template style="display: block" #default="scope">
                                <el-button
                                        size="small"
                                        type="success"
                                        @click="editStudentLeaveLog(scope.row)"
                                >编辑
                                </el-button
                                >
                                <el-button
                                        size="small"
                                        type="danger"
                                        @click="deleteStudentLeaveLog(scope.row.id)"
                                >删除
                                </el-button
                                >
                            </template>
</el-table-column> -->
                        <template v-slot:empty>
                            <el-empty description="暂无数据"></el-empty>
                        </template>
                    </el-table>
                    <el-config-provider :locale="locale">
                        <el-pagination :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
                            layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
                            @current-change="changeCurrentPage">
                        </el-pagination>
                    </el-config-provider>
                </div>
                <bottomView />
            </div>
        </div>
    </el-main>
</template>
<script>
import { ElConfigProvider, ElMessageBox, ElMessage, ElLoading } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";
import bottomView from "@/components/bottom.vue";
import { API_BASE_NAME2, API_BASE_NAME3 } from '../../constants';
import { formatTime } from '@/utils/dateFormat';

export default {
    name: "StudentLeaveLogListView",
    data() {
        return {
            type: 0,
            title: "",
            page: 0,
            pageSize: 10,
            total: 0,
            limit: 10,
            list: [],
            form: {
                studentName: "",
                studentNumber: "",
                typeName: "",
                campus: '',
                classId: "",
                gradeId: "",
                startTimeStr: "",
                endTimeStr: "",
            },
            dateValue: [],
            startTime: '',
            endTime: '',
            campusData: [
                {
                    label: API_BASE_NAME2,
                    value: 1,
                },
                {
                    label: API_BASE_NAME3,
                    value: 2,
                },
            ],
            gradeIds: [],
            classInfos: [],
            map: {},
        };
    },
    components: {
        IconPicture,
        ElConfigProvider,
        ElMessageBox,
        ElMessage, Upload, bottomView
    },
    setup() {
        return {
            locale: zhCn,
        };
    },
    mounted() {
        this.getDate()
        this.getStudentLeaveLogList();
        this.getGradeIds();
        this.getBaseClassInfoList();
    },
    watch: {
        $route() {
            this.getStudentLeaveLogList();
            this.getGradeIds();
            this.getBaseClassInfoList();
        },
    },
    methods: {
        getGradeIds() {
            let date = new Date();
            let nowYear = parseInt(date.getFullYear());
            let nowMonth = parseInt(date.getMonth() + 1);
            let gradeIds = [];
            if (nowMonth > 6) {
                gradeIds.push(nowYear - 2);
                gradeIds.push(nowYear - 1);
                gradeIds.push(nowYear);
            } else {
                gradeIds.push(nowYear - 3);
                gradeIds.push(nowYear - 2);
                gradeIds.push(nowYear - 1);
            }
            this.gradeIds = gradeIds;
        },
        getBaseClassInfoList() {
            httpService("/javaApi/baseClassInfo/getAllEffectClass", {}, "get").then(
                (data) => {
                    this.map = data;
                }
            );
        },
        filterGradeId(val) {
            this.classInfos = this.map[val];
        },
        getDate() {
            // let currentDate = new Date();
            // this.endTime = currentDate.toISOString().slice(0, 10);
            // currentDate.setDate(1);
            // this.startTime = currentDate.toISOString().slice(0, 10);
            // this.dateValue = [this.startTime, this.endTime]
        },
        changeDate(value) {
            // console.log(value);
            if (value) {
                this.form.startTimeStr = formatTime(value[0])
                this.form.endTimeStr = formatTime(value[1])
            } else {
                this.form.startTimeStr = ""
                this.form.endTimeStr = ""
            }
            console.log(this.form.startTimeStr, this.form.endTimeStr);
        },
        searchEnter: function (e) {
            // 使用 which 和 keyCode 属性来解决兼容问题
            var keyCode = window.event ? e.keyCode : e.which;
            var val = e.target.value;
            if (keyCode == 13 && val) {
                this.getStudentLeaveLogList();
            }
        },
        add() {
            this.$refs.addEditStudentLeaveLogView.title = "添加"
            this.$refs.addEditStudentLeaveLogView.personnelType = this.type
            this.$refs.addEditStudentLeaveLogView.dialogFormVisible = true;
        },
        editStudentLeaveLog(obj) {
            this.$refs.addEditStudentLeaveLogView.title = "修改信息"
            this.$refs.addEditStudentLeaveLogView.getStudentLeaveLogById(obj.id);
            this.$refs.addEditStudentLeaveLogView.dialogFormVisible = true;
        },
        deleteStudentLeaveLog(id) {
            ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    httpService("/javaApi/studentLeaveLog/deleteById", "id=" + id, "post").then(
                        (data) => {
                            if (data.code == 200) {
                                ElMessage.success("删除成功");
                                this.getStudentLeaveLogList();
                            } else {
                                ElMessage.error("删除失败，请重试！");
                            }
                        }
                    );
                })
                .catch(() => {
                });
        },
        reloadData() {
            this.page = 1;
            this.getStudentLeaveLogList()
        },
        importExcel() {
            this.$refs.addFileView.dialogFormVisible = true;
            this.$refs.addFileView.title = "批量导入";
            this.$refs.addFileView.uploadApiUrl = "/javaApi/studentLeaveLog/importExcel";
        },
        exportExcel() {
            var loadingInstance = ElLoading.service({
                lock: true, text: '加载中', background: 'rgba(0, 0, 0, 0.7)',
            });
            this.form.classId ? this.form.classId : this.form.classId = ""
            this.form.gradeId ? this.form.gradeId : this.form.gradeId = ""
            this.form.campus ? this.form.campus : this.form.campus = ""
            httpService(
                "/javaApi/studentLeaveLog/exportExcel",
                {
                    "studentName": this.form.studentName,
                    "studentNumber": this.form.studentNumber,
                    "typeName": this.form.typeName,
                    "campus": this.form.campus,
                    "classId": this.form.classId,
                    "gradeId": this.form.gradeId,
                    "startTimeStr": this.form.startTimeStr,
                    "endTimeStr": this.form.endTimeStr,
                },
                "get"
            ).then((data) => {
                loadingInstance.close()
                if (data.code == 200) {
                    window.open('https://wx.zhengzhong.cn' + data.message, '_blank')
                }
            });
        },
        onSubmit() {
            this.getStudentLeaveLogList();
        },
        // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
        changePageSize(size) {
            this.pageSize = size;
            this.limit = size;
            this.getStudentLeaveLogList();
        },

        // 改变页码，page：回调参数，表示当前选中的“页码”
        changeCurrentPage(page) {
            this.page = page;
            this.getStudentLeaveLogList();
        },
        tableRowClassName({ row, rowIndex }) {
            return "";
        },
        getStudentLeaveLogList() {
            this.form.classId ? this.form.classId : this.form.classId = ""
            this.form.gradeId ? this.form.gradeId : this.form.gradeId = ""
            this.form.campus ? this.form.campus : this.form.campus = ""
            httpService(
                "/javaApi/studentLeaveLog/list/" + this.page + "/" + this.pageSize,
                {
                    "studentName": this.form.studentName,
                    "studentNumber": this.form.studentNumber,
                    "typeName": this.form.typeName,
                    "campus": this.form.campus,
                    "classId": this.form.classId,
                    "gradeId": this.form.gradeId,
                    "startTimeStr": this.form.startTimeStr,
                    "endTimeStr": this.form.endTimeStr,
                },
                "get"
            ).then((data) => {
                this.list = data.list;
                this.total = data.total;
                this.pageCount = data.pages;
            });
        },
    },
};
</script>
<style scoped>
.el-main {
    padding: 0px;
}

.teacher-content {
    width: 100%;
}

.app-table {
    background: #eff3f5;
    padding: 10px;
}

.app-search {
    /* padding-top: 20px; */
    /* border-top: 1px solid #dedede; */
    /* margin-top: 20px; */
}

.app-separator {
    padding-top: 10px;
    padding-bottom: 20px;
    padding-left: 10px;
}

.app-body {
    background: white;
    padding: 20px 20px 10px 20px;
    margin-bottom: 10px;
    min-height: calc(100vh - 216px);
}

.el-table .warning-row {
    --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
    --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
    text-align: center;
    margin-top: 20px;
}

.serach_btn {
    background-color: #007EFF;
    color: #fff;
    border-radius: 8px;
    border: none;
}

.export_btn {
    background-color: #FF7200;
    border-radius: 8px;
    color: #fff;
    border: none;
}
</style>
